<template>
    <Modal
        v-model="modal.show"
        :title="modal.title"
        :loading="modal.loading"
        @on-ok="saveContent"
        @on-cancel="hideModal"
    >
        <DataForm ref="formValidate" :data="cFormData" :label-width="80"></DataForm>
    </Modal>
</template>
<script>
import {
    addProject,
    updateProject,
} from '../api/projectManage.js';
import { mapState } from 'vuex';
// import { typeRecordMixin } from '../mixin/research';
import DataForm from '../dataForm'
// import { formDataMixin } from './formData.js'
// import FormModal from '../dataForm/formModal.vue'
export default {
    name: 'EditModal',
    components: { DataForm }, //FormModal
    // mixins: [formDataMixin], //treeDirectionData
    // inject: ['parent'],
    props: {
        title: {
            type: String,
            default: '项目管理',
        },
        formData:{
            type: Object,
            default: () => {},
        }
    },
    data() {
        return {
            modal: {
                show: false,
                data: '',
                loading: true,
            },
            allDesignerList: [],
            cFormData:this.deepCopy(this.formData)
        };
    },
    computed: {
        ...mapState('user', ['nickname', 'userId', 'isRootDesigner'])
    },
    watch: {
        
    },
    mounted() {
        // this._formValidate = { ...this.formValidate};
        // this.getKyType();
    },
    methods: {
        deepCopy(obj){
            if (typeof obj !== 'object' || obj === null) {
                return obj;
            }
            let newObj = Array.isArray(obj) ? [] : {};
            for (let key in obj) {
                if (Object.prototype.hasOwnProperty.call(obj, key)) {
                    newObj[key] = this.deepCopy(obj[key]);
                }
            }
            return newObj;
        },
        showModal(data) {
            // this.getCompanyUserList();
            let name = this.title;
            this.modal.show = true;
            this.op = data ? 'update' : 'add';
            if (!data) {
                this.modal.title = `创建${name}`;
                this.resetField();
            } else {
                this.modal.title = `修改${name}`;
                this.detail = data;
                this.detail.time = [data.startTime,data.endTime];
                Object.keys(this.cFormData).forEach(key => {
                    this.cFormData[key].value = this.detail[key]
                })
            }
        },
        changeDirection(name) {
            this.filterTypeWithDirection(name);
            this.formValidate.typeCode = '';
        },
        resetField() {
            this.cFormData = this.deepCopy(this.formData);
            this.$refs.formValidate.resetFields();
        },
        saveContent() {
            this.$refs.formValidate.validate((valid) => {
                if (valid) {
                    this.formValidate = valid;
                    this.formValidate.startTime = valid.time[0];
                    this.formValidate.endTime = valid.time[1];
                    delete this.formValidate.time;
                    let params, reqApi, txt;
                    if (this.op == 'add') {
                        let params = { ...this.formValidate };
                        reqApi = addProject(params);
                        txt = '创建';
                    } else {
                        params = { ...this.detail,...this.formValidate };
                        reqApi = updateProject(params.id, params);
                        txt = '修改';
                    }
                    reqApi
                        .then(({ code, data, message }) => {
                            if (code === 1) {
                                this.$Message.info(`${txt}成功`);
                                this.hideModal();
                                this.$emit('ok', this.op);
                            } else {
                                this.$Message.error(message);
                            }
                            this.cancelLoading();
                        })
                        .catch((error) => {
                            this.cancelLoading();
                        });
                } else {
                    this.cancelLoading();
                }
            });
        },
        hideModal() {
            this.modal.show = false;
        },
        cancelLoading() {
            this.modal.loading = false;
            setTimeout(() => {
                this.modal.loading = true;
            });
        },
        uploadSuccess(val, type) {
            this.formValidate.iconPath = val.filePath;
        },
        setClear() {
            this.formValidate.iconPath = '';
        },
        onEditorChange(val){
            this.formValidate.description = val;
        }
    },
};
</script>
<style scoped></style>
